package Readers;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

//import jxl.Sheet;
//import jxl.Workbook;
import NWPackage.Calculations;
import NWPackage.OneSettingDistMatrix;
import NWPackage.ProbabilityCell;
import NWPackage.RealVoteCell;
import NWPackage.SkewnessMap;
import ProbabilitiesCalcs.DynamicProbCalc;
import Writers.MsgLog;
import org.apache.poi.hssf.usermodel.HSSFSheet;
//import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
//import org.apache.poi.hssf.usermodel.HSSFRow;
//import org.apache.poi.hssf.usermodel.HSSFCell;
import java.io.*; 
//import java.util.Iterator;

public  class SkewnessInput{
	
	private final String THIS_FOLDER = System.getProperty("user.dir")+"\\skewness\\";
	
	private String inputFile;
	private SkewnessMap skewnessMap;
	private int numCandidates;
	@SuppressWarnings("unused")
	private int numVotes;
	//private int numAgents;
	//private List<String> agents = new ArrayList<String>();
	private List<String> dynamicCandidates;
	//private OneSettingDistMatrix inputMatrix;
	//private Workbook w;
	private HSSFWorkbook w;
	private File inputWorkbook;
	private ProbabilityCell[] allprobabilities = null;
	//private RealVoteCell[] realVoteInputMatrix;
	//private boolean isSimulated = false;
	//private String dataType = "";
	//private int huristic;

	public SkewnessInput(String inputFile) throws Exception
	{
		this.inputFile = inputFile;
		
		inputWorkbook = new File(THIS_FOLDER+"\\"+inputFile);
		try {
			read();
		} catch (Exception e) {
			System.out.println("Error in skewness reading the Excel File");
			MsgLog.write_to_init("Error in reading the skewness Excel File");
			throw new IOException("Error in reading the skewness Excel File");
		}
	}
	
	private void read() throws Exception {
		try{
			w = new HSSFWorkbook(new FileInputStream(inputWorkbook));
			//get the first sheet
			//get the Inputs:
		
			//numCandidates = 5;//(int)sheet.getRow(1).getCell(1).getNumericCellValue();
			//numVotes = numCandidates;//(int)sheet.getRow(2).getCell(1).getNumericCellValue();
			
			
			try{
				readConfigSheet();
				//read the skewness probs - into 'skewnessMatrix'
				readSkewnessMap();
				dynamicCandidates = skewnessMap.getCandidatesList();
			
			}
			catch(Exception e)
			{
				//trow exception - read methode unknow
				throw new Exception("please specify one of the reading methods: 'N'-Netflix, 'S'-Simulated, 'Sushi'-probs for each permutation" );
			}
			//read the real votes matrix from Excel
			//readRealVotesMatrix();
			
			
		}
		catch (Exception e)
		{
			MsgLog.write_to_init(e.getMessage());
			System.out.println(e.getMessage());
			throw e;
			
		}
		
	}


	private void readConfigSheet() {
	//	MsgLog.write("Reading all configu sheet from skewness file Excel");
		HSSFSheet sheet = w.getSheetAt(0);
		numCandidates = (int) sheet.getRow(0).getCell(1).getNumericCellValue();
		numVotes = numCandidates;
	}

	private void readSkewnessMap() {
	//	MsgLog.write("Reading all skewness table directly from Excel");
		HashMap<Integer, List<Double>> allSkewnessProbs = new HashMap<Integer, List<Double>>();
		HSSFSheet sheet = w.getSheetAt(1);
		//loop over all cells
		for (int cell = 0; sheet.getRow(0).getCell(cell).getNumericCellValue()<999; cell++) {
			Integer key = (int) sheet.getRow(0).getCell(cell).getNumericCellValue();
			List<Double> probs = new ArrayList<Double>();
			//loop over all rows for this key
			for (int row = 1; row<=Calculations.calcCandidatesPermutationsNPR(numCandidates); row++) {
				probs.add(sheet.getRow(row).getCell(cell).getNumericCellValue());
			}
			
			allSkewnessProbs.put(key, probs);
		}
		skewnessMap = new SkewnessMap(numCandidates, allSkewnessProbs);
	}

	/*private void readInputMatrix_Susi() {
		MsgLog.write("Reading the probs directly from Excel  - simulated cells");
		int permutationsCells = Calculations.calcCandidatesPermutationsNPR(numCandidates);
		allprobabilities = new ProbabilityCell[numAgents*permutationsCells];
		int index = 0;
		HSSFSheet sheet = w.getSheetAt(1);
		//loop over all the agents
		for (int agent_index = 1; agent_index <= numAgents; agent_index++) {
			String agentName = String.valueOf(sheet.getRow(index+1).getCell(0).getNumericCellValue());
			agents.add(agentName);
			//loop over all permutations
			for (int i = 0; i < permutationsCells; i++) {
				//read the permutation cells
				int lastCell = sheet.getRow(index+1).getPhysicalNumberOfCells();
				List<String> candidates = readList(sheet,index+1);
				//read the probability
				double probability = sheet.getRow(index+1).getCell(lastCell-1).getNumericCellValue();
				
				ProbabilityCell newCell = new ProbabilityCell(agentName, probability, candidates);
				allprobabilities[index] = newCell;
				allprobabilities[index].setIndex(index);
				index++;
			}
		}
		
	}*/
	//create order by '<'
	private List<String> readList(HSSFSheet sheet, int rowNumber) {
		String[] permutation = new String[numCandidates];
		for (int cellIndex = 1; cellIndex <= numCandidates; cellIndex++  ) {
			permutation[cellIndex-1] = sheet.getRow(rowNumber).getCell(cellIndex).getStringCellValue();
		}
		return Arrays.asList(permutation);
	}
	
	private SkewnessMap getSkewnessMap(){
		return this.skewnessMap;
	}
	public  List<String> getCandidatesList(){
		return this.skewnessMap.getCandidatesList();
	}
	public List<ProbabilityCell> buildSkewnessListForAgent(String agent, int key) throws IOException
	{
		try {
			return this.skewnessMap.buildSkewnessListForAgent(agent, key);
		} catch (Exception e) {
			System.out.println("please give valid skewness key");
			throw new IOException("please give valid skewness key");
		}
	}

	

}
